// pages/evidence/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headTop: getApp().globalData.headTop,
    show1: false,
    // 所有街道
    allParkingLotList: [],
    parkingName: '',
    show2: false,
    allParkingSpace: [],
    parkingSpaceName: '',
    params: {
      parkingLotId: '',
      parkingSpaceId: '',
      tollCollectorId: 100,
      IllegalLicensePlate: '',
      IllegalLicensePlatePhotoId: '',
      IllegalPhotoId: []
    },
    fileList: []
  },
  // 新增违章取证记录
  addEvidenceRecord() {
    if (this.data.params.IllegalLicensePlate == '') {
      wx.showToast({
        title: '请先识别违章车牌',
        icon: 'none'
      })
      return
    }
    if (this.data.params.IllegalPhotoId.length == 0) {
      wx.showToast({
        title: '请上传违章记录照片',
        icon: 'none'
      })
      return
    }
    let params = this.data.params
    params.IllegalPhotoId = params.IllegalPhotoId.join(',')
    wx.showToast({
      title: '提交成功',
      icon: 'none'
    })
  },
  // 上传违章记录图片
  afterRead2(event) {
    const {
      file
    } = event.detail;
    // 上传完成需要更新 fileList
    let fileList = this.data.params.IllegalPhotoId;
    fileList.push({
      ...file
    });
    this.setData({
      'params.IllegalPhotoId': fileList
    });
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'uploadImg', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        // 上传完成需要更新 fileList
        const {
          fileList = []
        } = this.data;
        fileList.push({
          ...file,
          url: res.data
        });
        this.setData({
          fileList
        });
      },
    });
  },
  // 点击右侧去拍照
  chooseImage(e) {
    let that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        console.log(resizeBy)
        const {
          fileList = []
        } = this.data;
        fileList.push({
          ...res.tempFiles
        });
        this.setData({
          fileList,
          'params.IllegalLicensePlate': '苏a12345'
        });
      }
    })
  },
  // 上传违章车牌图片
  afterRead(event) {
    const {
      file
    } = event.detail;
    // 上传完成需要更新 fileList
    const {
      fileList = []
    } = this.data;
    fileList.push({
      ...file
    });
    this.setData({
      fileList,
      'params.IllegalLicensePlate': '苏a12345'
    });
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'uploadImg', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        // 上传完成需要更新 fileList
        const {
          fileList = []
        } = this.data;
        fileList.push({
          ...file,
          url: res.data
        });
        this.setData({
          fileList
        });
      },
    });
  },
  getallParkingLotList() {
    // 模拟接口查询街道
    this.setData({
      allParkingLotList: [{
        parkingLotId: 100,
        parkingName: '测试停车场'
      }]
    })
  },
  // 选择某街道
  onConfirm(e) {
    console.log(e)
    this.onClose()
    if (e.detail.value.parkingLotId) {
      // 选择街道
      this.setData({
        "params.parkingLotId": e.detail.value.parkingLotId,
        parkingName: e.detail.value.parkingName,
        "params.parkingSpaceId": '',
        parkingSpaceName: ''
      })
      this.getallParkingSpace(e.detail.value.parkingLotId)
    } else if (e.detail.value.parkingSpaceId) {
      // 选择车位
      this.setData({
        "params.parkingSpaceId": e.detail.value.parkingSpaceId,
        parkingSpaceName: e.detail.value.parkingSpaceName
      })
    }

  },
  getallParkingSpace(parkingLotId) {
    // 查询某停车场的所有车位
    let params = {
      parkingLotId
    }
    this.setData({
      allParkingSpace: [{
        parkingSpaceId: 100,
        parkingSpaceName: '测试001车位'
      }]

    })
  },
  // 显示选择街道
  showPopup(e) {
    console.log(e)
    const {
      index
    } = e.currentTarget.dataset
    if (index == 2 && this.data.parkingName == '') {
      wx.showToast({
        title: '请先选择街道',
        icon: 'none'
      })
      return
    }
    this.setData({
      [`show${index}`]: true
    });
  },
  // 关闭弹窗
  onClose() {
    this.setData({
      show1: false,
      show2: false,
      show3: false
    });
  },
  // 返回首页
  onClickLeft() {
    wx.switchTab({
      url: '/pages/work/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getallParkingLotList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1,
        list: [{
            "pagePath": "/pages/record/index",
            "text": "违章记录",
            "iconPath": "/images/tabbar/record.png",
            "selectedIconPath": "/images/tabbar/recorda.png"
          },
          {
            "pagePath": "/pages/evidence/index",
            "text": "违章取证",
            "iconPath": "/images/tabbar/evidence.png",
            "selectedIconPath": "/images/tabbar/evidencea.png"
          }
        ]
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})